package com.xiaojian.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.xiaojian.blog.constant.WebConstant;
import com.xiaojian.blog.controller.BaseController;
import com.xiaojian.blog.dto.LogAction;
import com.xiaojian.blog.dto.Types;
import com.xiaojian.blog.exception.TipException;
import com.xiaojian.blog.model.ContentPO;
import com.xiaojian.blog.model.ContentPOExample;
import com.xiaojian.blog.model.MetaPO;
import com.xiaojian.blog.model.UserPO;
import com.xiaojian.blog.model.VO.RestResponseVO;
import com.xiaojian.blog.service.ContentService;
import com.xiaojian.blog.service.LogService;
import com.xiaojian.blog.service.MetaService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/article")
@Transactional(rollbackFor = TipException.class)
public class ArticleController extends BaseController {

    private Logger LOGGER = LoggerFactory.getLogger(ArticleController.class);

    @Resource
    private ContentService contentService;

    @Resource
    private MetaService metaService;

    @Resource
    private LogService logService;

    @GetMapping(value = "/publish")
    public String newArticle(HttpServletRequest request) {
        List<MetaPO> categories = metaService.getMetas(Types.CATEGORY.getType());
        request.setAttribute("categories", categories);
        return "admin/article_edit";
    }

    @GetMapping(value = "")//文章管理
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "15") int limit,
                        HttpServletRequest request) {
        ContentPOExample contentPOExample = new ContentPOExample();
        contentPOExample.setOrderByClause("created desc");
        contentPOExample.createCriteria().andTypeEqualTo(Types.ARTICLE.getType());
        PageInfo<ContentPO> articlesWithpage = contentService.getArticlesWithpage(contentPOExample, page, limit);
        request.setAttribute("articles", articlesWithpage);
        return "admin/article_list";
    }

    /**
     * 发布文章的方法post请求
     */
    @PostMapping(value = "/publish")
    @ResponseBody
    public RestResponseVO publishArticle(ContentPO contentPO, HttpServletRequest request) {
        UserPO user = this.user(request);
        contentPO.setAuthorId(user.getUid());
        contentPO.setType(Types.ARTICLE.getType());
        if (StringUtils.isBlank(contentPO.getCategories())) {
            contentPO.setCategories("默认分类");
        }
        String result = contentService.publish(contentPO);
        if (!WebConstant.SUCCESS_RESULT.equals(result)) {
            return RestResponseVO.fail(result);
        }
        return RestResponseVO.ok();
    }

    @GetMapping(value = "/{cid}")//文章管理界面编辑文章
    public String editArticle(@PathVariable String cid, HttpServletRequest request) {
        //通过cid查出文章
        ContentPO contents = contentService.getContents(cid);
        request.setAttribute("contents", contents);
        List<MetaPO> metas = metaService.getMetas(Types.CATEGORY.getType());
        request.setAttribute("categories", metas);
        request.setAttribute("active", "article");
        return "admin/article_edit";
    }

    /**
     * 删除文章的POST请求
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public RestResponseVO delete(@RequestParam int cid,
                                 HttpServletRequest request) {
        String result = contentService.deleteByCid(cid);
        //删除文章的操作要记录到日志中去
        logService.insertLog(LogAction.DEL_ARTICLE.getAction(), cid + "", request.getRemoteAddr(), this.getUid(request));
        if (!WebConstant.SUCCESS_RESULT.equals(result)) {
            return RestResponseVO.fail(result);
        }
        return RestResponseVO.ok();
    }
}
